/******************************************************************************
 *  Copyright 2013 Bernhard Grünewaldt                                        *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package com.google.code.greenwood.confluencemavenreleaseinfomacro.pojo;

import java.util.Collection;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class ArtifactInformation implements Cloneable {

	private String name;

	private String groupid;
	
	private String artifactid;
	
	private String version;

	private String packaging;
	
	
	public String getDownloadUrl(String baseUrlToMavenRepo) {
		if (baseUrlToMavenRepo == null  || baseUrlToMavenRepo.isEmpty()) {
			return null;
		}
		StringBuilder url = new StringBuilder();
		url.append(baseUrlToMavenRepo);
		if (!baseUrlToMavenRepo.endsWith("/")) {
			url.append("/");	
		}		
		groupid = groupid.replaceAll("\\.", "/");
		url.append(groupid);
		url.append("/");
		url.append(artifactid);
		url.append("/");
		url.append(version);
		url.append("/");
		url.append(artifactid);
		url.append("-");
		url.append(version);
		url.append(".");
		url.append(packaging);
		return url.toString();
	}	
	
	public String getSourceUrl(String baseUrlToMavenRepo) {
		if (baseUrlToMavenRepo == null  || baseUrlToMavenRepo.isEmpty()) {
			return null;
		}
		StringBuilder url = new StringBuilder();
		url.append(baseUrlToMavenRepo);
		if (!baseUrlToMavenRepo.endsWith("/")) {
			url.append("/");	
		}		
		groupid = groupid.replaceAll("\\.", "/");
		url.append(groupid);
		url.append("/");
		url.append(artifactid);
		url.append("/");
		url.append(version);
		url.append("/");

		return url.toString();
	}	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getGroupid() {
		return groupid;
	}

	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}

	public String getArtifactid() {
		return artifactid;
	}

	public void setArtifactid(String artifactid) {
		this.artifactid = artifactid;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getPackaging() {
		return packaging;
	}

	public void setPackaging(String packaging) {
		this.packaging = packaging;
	}	
	
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((artifactid == null) ? 0 : artifactid.hashCode());
		result = prime * result + ((groupid == null) ? 0 : groupid.hashCode());
		result = prime * result
				+ ((packaging == null) ? 0 : packaging.hashCode());
		return result;
	}

	/**
	 * equals Method must only compare groupId, artifactId and Packaging!
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArtifactInformation other = (ArtifactInformation) obj;
		if (artifactid == null) {
			if (other.artifactid != null)
				return false;
		} else if (!artifactid.equals(other.artifactid))
			return false;
		if (groupid == null) {
			if (other.groupid != null)
				return false;
		} else if (!groupid.equals(other.groupid))
			return false;
		if (packaging == null) {
			if (other.packaging != null)
				return false;
		} else if (!packaging.equals(other.packaging))
			return false;
		return true;
	}

	/**
	 * equals Method must only compare groupId, artifactId and Packaging!
	 */
	public static ImmutableSet<ArtifactInformation> getUnionOfModuleReleaseVersionDependenciesLists(Collection<ArtifactInformation> moduleReleaseVersionOneDependencies, Collection<ArtifactInformation> moduleReleaseVersionTwoDependencies) {
	    Set<ArtifactInformation> firstSet = ImmutableSet.copyOf(moduleReleaseVersionOneDependencies);
	    Set<ArtifactInformation> secondSet = ImmutableSet.copyOf(moduleReleaseVersionTwoDependencies);
	
	    SetView<ArtifactInformation> unionView = Sets.union(firstSet, secondSet);
	    ImmutableSet<ArtifactInformation> finalSet = unionView.immutableCopy();
	    
	    return finalSet;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		ArtifactInformation clone = new ArtifactInformation();
		clone.setArtifactid(this.artifactid);
		clone.setGroupid(this.groupid);
		clone.setName(this.name);
		clone.setPackaging(this.packaging);
		clone.setVersion(this.version);
		return clone;
	}
	
	
	
	
}
